package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Product;
import cn.wolfcode.mapper.ProductMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.pageResult;
import cn.wolfcode.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ProdcutServiceImpl
 * @Description TODO
 * @Author wdx
 * @Date 2023/3/31 0:19
 */
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Transactional
    @Override
    public void delete(Long id) {
        productMapper.delete(id);
    }

    @Override
    public void insert(Product product) {
    productMapper.insert(product);
    }

    @Override
    public Product selectById(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    public List<Product> selectAll() {
        return productMapper.selectAll();
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
    }

    @Override
    public pageResult<Product> Query(QueryObject qo) {
        int count = productMapper.selectForCount(qo);
        if(count <= 0){
            return  new pageResult<Product>(qo.getCurrentPage(),qo.getPageSize(),count,new ArrayList());
        }
        List<Product> products = productMapper.queryPage(qo);
        return new pageResult<>(qo.getCurrentPage(),qo.getPageSize(),count,products);
    }


}
